var isLogining = false, passwordLogin = true, delayTime = 0;
var usernameCtl = $("#username"), userphoneCtl = $("#userphone"), passwordCtl = $("#password"), vericodeCtl = $("#vericode"), codebtnCtl = $("#codebtn");

$(function() {
	//回车响应事件
	$(document).keydown(function(event) {
		if(event.keyCode == 13 && !isLogining) {
			onLoginClick();
		}
	});
});

//验证码点击事件
function onVericodeClick() {
	var src = __ctx + "/nr/vericode.xhtml?t=" + new Date().getTime();
	$("#codeimg").attr("src", src);
}

//获取验证码点击事件
function onCodebtnClick() {
	if (codebtnCtl.hasClass("disabled")) return;
	
	$.post(__ctx + '/nr/api/aliyun/getVercode.xhtml', {userphone : userphoneCtl.val()}, function(result) {
		if (result.status) {
			delayTime = 60;
			codebtnCtl.addClass("disabled");
			codebtnCtl.children("label").text(delayTime + "s");
			setTimeout(timeoutHandler, 1000);
		} else {
			SearchUtils.msg("发送验证码失败！");
		}
	});
}

//超时处理器
function timeoutHandler() {
	delayTime --;
	if (delayTime <= 0) {
		codebtnCtl.removeClass("disabled");
		codebtnCtl.children("label").text("");
		return;
	}
	
	codebtnCtl.children("label").text(delayTime + "s");
	setTimeout(timeoutHandler, 1000);
}

//模式切换点击事件
function onModeSwitchClick() {
	if (passwordLogin) {
		passwordLogin = false;
		
		$("#usernameContainer").addClass("hidden");
		$("#userphoneContainer").removeClass("hidden");
		$("#passwordContainer").addClass("hidden");
		$("#codeimg").addClass("hidden");
		$("#codebtn").removeClass("hidden");
		$(".blank-holder").removeClass("hidden");
		
		$("#switchbtn").text("密码登录");
	} else {
		passwordLogin = true;
		
		$("#usernameContainer").removeClass("hidden");
		$("#userphoneContainer").addClass("hidden");
		$("#passwordContainer").removeClass("hidden");
		$("#codeimg").removeClass("hidden");
		$("#codebtn").addClass("hidden");
		$(".blank-holder").addClass("hidden");
		
		$("#switchbtn").text("验证码登录");
		
		var src = __ctx + "/nr/vericode.xhtml?t=" + new Date().getTime();
		$("#codeimg").attr("src", src);
	}
}

//登录点击事件
function onLoginClick() {
	isLogining = true;
	
	var data = {
		username : usernameCtl.val(), 
		userphone : userphoneCtl.val(), 
		password : passwordCtl.val(), 
		vericode : vericodeCtl.val(), 
		loginmode : passwordLogin
	};
	pcGlobal.sendRequest(__ctx + "/nr/login.xhtml", data, function(data, status) {
		if (data.status) {
			if (!passwordLogin || data.result != "") window.location.replace(__ctx + "/main.xhtml");
			else {
				var data = {
					url : __ctx + "/nr/goLoginUpdatePassword.xhtml",
					title : "修改密码",
					width : 300,
					height : 320,
					onload : function(window) {
					},
					ondestroy : function(result){
						if (result == "ok") window.location.replace(__ctx + "/main.xhtml");
						isLogining = false;
					}
				}
				top.search.popDialog(data);
			}
		} else {
			search.warn({content : data.result, funl : function() {
				isLogining = false;
			}});
		}
	});
}

//重置点击事件
function onResetClick() {
	usernameCtl.val("");
	userphoneCtl.val("");
	passwordCtl.val("");
	vericodeCtl.val("");
}